const util = require('../../../utils/util.js');

const rate = wx.getSystemInfoSync().windowWidth / 750;
Page({

  /**
   * 页面的初始数据
   */

  data: {
    bgimg: getApp().globalData.imgUrls + 'activity_Popup.png',
    imgUrls: getApp().globalData.imgUrls,
    statue: 0,
    mask: false,
    drawPrize: false
  },
  add() {
    this.data.data.show_status = this.data.data.show_status + 1
    this.setData({
      data: this.data.data
    })
    console.log(this.data.data.show_status)
  },
  buy() { // 进入订单
    util.API('spwx/v1.Assemble/goToWin', {
      goods_id: this.data.data.goods_id
    }).then(res => {
      let {
        code,
          msg
      } = res
      if (code == 200) {
        let {
          data,
          jump_id
        } = this.data
        let {
          distribution_type,
          distribution_name,
          defaultIndex,
        } = res.data;
        distribution_name = JSON.stringify(distribution_name);
        activity_id = jump_id;
        let goods = [{
          goods_id: data.goods_id,
          goods_stock: 1,
          sku: data.sku.sku_id,
          uniqueKey: data.sku.uniqueKey
        }]
        wx.navigateTo({
          url: `/pages/DingdanPay/DingdanPay?distribution=${distribution_type}&distribution_name=${distribution_name}&defaultIndex=${defaultIndex}&activity_id=${activity_id}&market_type=6&goods=${JSON.stringify(goods)}&money=${data.money}`
        })
      } else {
        wx.showToast({
          title: msg + '',
          icon: 'none'
        })
      }
    })

    //     if (code === 200) {
    //       this.data.shop_goods_id = data.goods_id
    //       data.sku_type = JSON.parse(data.sku_type)
    //       data.goods_cover = data.goods_cover.split(",");
    //       data.defaultIndex = data.defaultIndex
    //       data.goods_content = data.goods_content.replace(/\<img/gi, '<img style="width:100%;height:100%;display:block;margin-top:-1px" ')
    //       var placeTypes = [];
    //       data.sku_type.forEach((items, indexs) => {
    //         placeTypes.push(items.type)
    //       })
    //       data.distribution_name = JSON.stringify(data.distribution_name)
    //       let waysArr = this.setWaysArr(this.data.waysArr, data.distribution_type, data.distribution_name)
    //       this.setData({
    //         distribution_name: JSON.parse(data.distribution_name),
    //         placeTypes,
    //         data,
    //         waysArr
    //       })
    //       console.log(this.data.distribution_name, 'distribution_name')
    //       console.log(this.data.data, 'data')
    //       // util.intervalTime(1562211922, this,1000)
    //     } else {
    //       wx.showToast({
    //         title: '该商品已经下架',
    //         icon: 'none',
    //         success() {
    //           setTimeout(() => {
    //             wx.navigateBack({
    //               delta: 1,
    //             })
    //           }, 1500)
    //         }
    //       })
    //     }
    //   })
    // },





  },
  joinDraw() {
    var that = this
    if (this.data.suo){
      return false
    }
    that.setData({
      suo: true
    })
    util.API('spwx/v1.Assemble/joinHot', {
      activity_id: this.data.jump_id,
      prize_id: this.data.prize.prize_id,
      type: 2,
      is_share: this.data.data.show_status,
      parent_id: this.data.is_share,
    }).then(res => {
      let {
        code,
        data,
        msg
      } = res
      that.setData({
        numberList: data,
        mask: true,
       
      })
      setTimeout(function () {
        that.setData({
          suo: false
        })
      }, 2000)
      that.details()
    })
  },
  drawPrize() {
    this.setData({
      drawPrize: !this.data.drawPrize
    })
  },
  preventTouchMove: function(event) {
    return false
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      jump_id: options.jump_id
    })
    this.data.is_share = options.member_id ? options.member_id : false
    var that = this;
    if (this.data.is_share) {
      wx.showToast({
        title: '点击抽奖,助力好友',
        icon: 'none',
        duration: 3000,
        mask: true,
      })
    }

  },
  details() {
    var that = this
    util.API('spwx/v1.Assemble/hotDetail', {
      hot_id: this.data.jump_id
    }).then(res => {
      let {
        code,
        data,
        msg
      } = res;

      data.prize.sku.sku_type_name = data.prize.sku.sku_type_name.split(',');
      data.prize.sku.attr_names = data.prize.sku.attr_names.split(',');
      util.intervalTime(data.etime, that, 1000);
      data.prize.goods.goods_content = data.prize.goods.goods_content.replace(/\<img/gi, '<img  style="width:100%;height:100%;display:block;" ');
      data.goods_id = data.prize.commodity_id
      data.sku = data.prize.sku
      data.money = data.prize.money
      console.log(data.prize.sku.sku_type_name)
      that.setData({
        data,
        numberList: data.my_code,
        prize: data.prize,
        sku: data.prize.sku,
        goods: data.prize.goods
      })
      var query = wx.createSelectorQuery();
      query.select('.stage').boundingClientRect(function(rect) {
        that.setData({
          indent: (rect.width / rate + 20) / 32
        })
      }).exec();




    })
  },
  // onPageScroll(){
  //   var query = wx.createSelectorQuery()//创建节点查询器 query
  //   query.select('.bannerImg').boundingClientRect()//这段代码的意思是选择Id=the-id的节点，获取节点位置信息的查询请求
  //       query.selectViewport().scrollOffset()//这段代码的意思是获取页面滑动位置的查询请求
  //       query.exec(function (res) {
  //       console.log(res[1])
  //       })
  // },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  preventTouchMove: function(event) {
    return false
  },
  Btn(e) {
    // var statue = e.currentTarget.dataset.statue;
    this.setData({
      mask: !this.data.mask
    })
    // switch (statue) {
    //   case '0':

    //     console.log(this.data.mask)
    //     break;
    //   default: this.setData({
    //     mask: false
    //   })
    // }
  },
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.details()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
    clearInterval(this.data.interval);
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {
    var that = this
    wx.showModal({
      title: '提示',
      content: '好友点击抽奖后,即可获得幸运号码',
      success: function(res) {
        console.log(res.confirm)
        if (res.confirm) {
          that.setData({
            mask: res.confirm
          })
        }
      }
    })

    var share = this.data.data.share ? this.data.data.share : {}
    return {
      title: share.title,
      desc: share.content,
      imageUrl: getApp().globalData.imgUrls + share.img,
      path: 'elsePage/pages/zeroYuanDetails/zeroYuanDetails?' + `&member_id=${getApp().globalData.member_id}&jump_id=${this.data.jump_id}`,
      success: function() {},
      fail: function() {

      },
    }
  }
})